package org.example.infrastructure.util;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.springframework.stereotype.Component;

import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

/**
 * mybatis类生成工具
 */
@Component
public class MybatisGenerator {

    public static void main(String[] args) {
        MybatisGenerator mybatisGenerator = new MybatisGenerator();
        mybatisGenerator.generatorCode();
    }

    private void generatorCode(){
        String url="jdbc:mysql://localhost:3306/demo?serverTimezone=GMT%2B8";
        List<String> tableNames = Arrays.asList("user"); // 添加你的表名到这个列表中

        for (String tableName : tableNames) {
            generateClassFile(url, tableName);
            generateXmlFile(url, tableName);
        }
    }

    /**
     * 生成xml文件
     * @param url
     * @param tableName
     */
    private void generateXmlFile(String url, String tableName) {
        FastAutoGenerator.create(url, "root", "")
                .globalConfig(builder -> builder
                        .author("example")
                        .disableOpenDir()
                        .outputDir(Paths.get(System.getProperty("user.dir")) +"/web-mybatis-example"
                                + "/src/main/resources")
                        .commentDate("yyyy-MM-dd")
                        .dateType(DateType.ONLY_DATE) // 设置时间类型策略
                )
                .strategyConfig(builder -> {
                    builder.addInclude(tableName) // 使用 tableName 变量
                            //实体对象生成配置
                            .entityBuilder()
                            .disable()
                            //repository对象生成配置
                            .serviceBuilder()
                            .disable()
                            //mapper对象生成配置
                            .mapperBuilder()
                            .disableMapper()
                            .mapperXmlTemplate("/mybatis/templates/mapper.xml")
                            //controller对象生成配置
                            .controllerBuilder()
                            .disable()
                    //.controllerBuilder()
                    //.enableRestStyle() // 启用 REST 风格
                    ;
                })
                .packageConfig(builder -> builder
                        .parent("")
                        .entity("infrastructure.persistence."+ tableName +".po")
                        .mapper("com.example.infrastructure.persistence."+ tableName +".repository")
                        .xml("mybatis/mapper")
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

    /**
     * 生成java类
     * @param url
     * @param tableName
     */
    private static void generateClassFile(String url, String tableName) {
        FastAutoGenerator.create(url, "root", "")
                .globalConfig(builder -> builder
                        .author("example")
                        .disableOpenDir()
                        .outputDir(Paths.get(System.getProperty("user.dir")) +"/web-mybatis-example"
                                + "/src/main/java")
                        .commentDate("yyyy-MM-dd")
                        .dateType(DateType.ONLY_DATE) // 设置时间类型策略
                )
                .strategyConfig(builder -> {
                    builder.addInclude(tableName) // 使用 tableName 变量
                            //实体对象生成配置
                            .entityBuilder()
                            .javaTemplate("/mybatis/templates/entity") // 使用自定义模板，省略了后缀ftl
                            .convertFileName((entityName) -> {
                                return entityName + "PO";
                            })
                            .enableLombok() // 启用 Lombok
                            .enableTableFieldAnnotation() // 启用字段注解
                            //repository对象生成配置
                            .serviceBuilder()
                            .serviceTemplate("/mybatis/templates/repository")
                            .convertServiceFileName((entityName) -> {
                                return entityName + "Repository";
                            })
                            //repositoryImpl对象生成配置
                            .serviceImplTemplate("/mybatis/templates/repositoryImpl") // 使用自定义模板
                            .convertServiceImplFileName((entityName) -> {
                                return entityName + "RepositoryImpl";
                            })
                            //mapper对象生成配置
                            .mapperBuilder()
                            .mapperTemplate("/mybatis/templates/mapper")
                            .disableMapperXml() // 禁用 XML 生成
                            //controller对象生成配置
                            .controllerBuilder()
                            .disable()
                            //.enableRestStyle() // 启用 REST 风格
                            ;
                })
                .packageConfig(builder -> builder
                        .parent("com.example")
                        .entity("infrastructure.persistence."+ tableName +".po")
                        .service("domain."+ tableName +".repository")
                        .serviceImpl("infrastructure.persistence."+ tableName +".repository")
                        .mapper("infrastructure.persistence."+ tableName +".repository")
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}